package cn.wolfcode.luowowo.website.controller;

import cn.wolfcode.luowowo.cache.service.IUserInfoCacheService;
import cn.wolfcode.luowowo.cache.service.IVerifyCodeService;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.common.util.Consts;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.annotation.RequiredLogin;
import com.alibaba.fastjson.JSON;
import org.apache.dubbo.config.annotation.Reference;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import java.util.UUID;

@Controller
public class LonginController {
    //判断手机号,注册,登录
    @Reference
    private IUserInfoService userInfoService;
    //验证码redis缓存
    @Reference
    private IVerifyCodeService verifyCodeService;
    //登录缓存.
    @Reference
    private IUserInfoCacheService userInfoCacheService;

    //检测手机号有没有
    @RequestMapping("/checkPhone")
    @ResponseBody
    public Boolean checkPhone(String phone) {

        return userInfoService.checkPhone(phone);

    }

    //注册
    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone, String nickname, String password, String rpassword, String verifyCode) {

        return userInfoService.userRegist(phone, nickname, password, rpassword, verifyCode);
    }


    //发送验证码
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone) {

        AjaxResult result = new AjaxResult();
        //先检查该手机号验证码是否有效
        if(verifyCodeService.getVerifyCode(phone)!=null){
            result.mark("验证码已经发送,不用重复发送");
        }

        //获取验证码
        String verifyCode = UUID.randomUUID().toString().substring(0, 4);
        //发送短信
        System.out.println(phone+"的验证码是"+verifyCode);
        //保存到reids
        verifyCodeService.setVerifyCode(phone,verifyCode);

        return result;
    }


    //登录

    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String phone, String password, HttpSession session, HttpServletResponse response) {
        AjaxResult result = userInfoService.userLogin(phone, password);
        //判断当期那操作是否登陆成功,成功把对象存入session和redis
        Object userInfo = result.getData();
        if(userInfo!=null){
            //把当期登录对象转入session中
            session.setAttribute(Consts.USER_INFO,userInfo);
            //把当期登录对象转为json存入redis
            String token = UUID.randomUUID().toString().replaceAll("-", "");
            //对象转化为json字符串.
            String json = JSON.toJSONString(userInfo);
            userInfoCacheService.setUserInfoCache(token,json);
            //把token以Cookie方式返回给客户.
            Cookie cookie = new Cookie(Consts.USER_INFO_TOKEN,token);
            cookie.setPath("/"); //访问路径为/
            response.addCookie(cookie);

        }



        return result;
    }

}
